package com.tust;

import java.util.ArrayList;
import java.util.Scanner;

public class TustStudentSystem {
    private static ArrayList<Student> studentList = new ArrayList<>();

    static {
        studentList.add(new Student("10101111","小李飞刀",18,"地球村"));
    }

    public static void startTustStudentSystem() {
        String menu;
        Scanner scanner = new Scanner(System.in);

        do {
            printMenu();
            menu = scanner.next();
            handleFun(menu, studentList);
        } while (!menu.equals("5"));
    }


    public static void printMenu() {
        System.out.println("----------欢迎来到Tust学生管理系统----------");
        System.out.println("1：添加学生");
        System.out.println("2：删除学生");
        System.out.println("3：修改学生");
        System.out.println("4：查询学生");
        System.out.println("5：退出");
        System.out.println("请输入您的选择：");
    }


    public static void handleFun(String menu, ArrayList<Student> arrayList) {
        switch (menu) {
            case "1" -> addStudent(arrayList);
            case "2" -> delStudent(arrayList);
            case "3" -> editStudent(arrayList);
            case "4" -> getStudent(arrayList);
            case "5" -> System.out.println("感谢您的使用，我们下次再见！");
            default -> System.out.println("查无此项，请重新输入！");
        }
    }

    private static void addStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);
        Student student = new Student();
        String sid;
        while (true) {
            System.out.println("请输入学号：");
            sid = scanner.next();
            if (!contains(arrayList, sid)) {
                break;
            }
            System.out.println("当前学号已存在，请重新输入！");
        }

        System.out.println("请输入姓名：");
        String name = scanner.next();
        System.out.println("请输入年龄：");
        int age = scanner.nextInt();
        System.out.println("请输入地址：");
        String address = scanner.next();

        student.setSid(sid);
        student.setName(name);
        student.setAge(age);
        student.setAddress(address);

        arrayList.add(student);

        System.out.println("学生信息添加成功！");

    }

    private static void delStudent(ArrayList<Student> arrayList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要删除学生的学号：");
        String sid = scanner.next();
        int index = getIndex(arrayList, sid);
        if (index == -1) {
            System.out.println("id不存在！");
            return;
        }

        arrayList.remove(index);

        System.out.println("删除学生信息成功！");
    }

    private static void editStudent(ArrayList<Student> arrayList) {
        System.out.println("你好，我是修改！");
    }

    private static void getStudent(ArrayList<Student> arrayList) {

        if (arrayList.size() == 0) {
            System.out.println("当前暂无学生信息，请录入后再查询");
            return;
        }

        System.out.println("id\t\t\t姓名\t\t年龄\t\t家庭住址");
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            System.out.println(student.getSid() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getAddress());
        }
    }

    private static boolean contains(ArrayList<Student> arrayList, String sid) {
        return getIndex(arrayList, sid) >= 0;
    }

    private static int getIndex(ArrayList<Student> arrayList, String sid) {
        for (int i = 0; i < arrayList.size(); i++) {
            Student student = arrayList.get(i);
            if (student.getSid().equals(sid)) {
                return i;
            }
        }
        return -1;
    }

}
